package com.rolerealm.service.impl;

import com.rolerealm.common.exception.BusinessException;
import com.rolerealm.common.constant.ErrorCode;
import com.rolerealm.dao.UserPreferenceMapper;
import com.rolerealm.domain.entity.UserPreferenceEntity;
import com.rolerealm.domain.vo.UserPreferenceVO;
import com.rolerealm.service.UserPreferenceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserPreferenceServiceImpl implements UserPreferenceService {

    private final UserPreferenceMapper userPreferenceMapper;

    @Override
    public UserPreferenceVO getPreferences(Long userId) {
        UserPreferenceEntity entity = userPreferenceMapper.selectByUserId(userId);
        if (entity == null) {
            // 初始化默认偏好
            entity = UserPreferenceEntity.builder()
                    .userId(userId)
                    .voiceSpeed(1.0)
                    .voicePitch(1.0)
                    .voiceVolume(1.0)
                    .languagePreference("zh-CN")
                    .autoPlayVoice(1)
                    .showTranscript(1)
                    .build();
            userPreferenceMapper.insert(entity);
        }
        UserPreferenceVO vo = new UserPreferenceVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public void updatePreferences(Long userId, UserPreferenceVO request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAM_MISSING);
        }
        UserPreferenceEntity entity = userPreferenceMapper.selectByUserId(userId);
        if (entity == null) {
            entity = UserPreferenceEntity.builder().userId(userId).build();
        }
        // 基本边界校验（可扩展至更严格范围）
        if (request.getVoiceSpeed() != null && (request.getVoiceSpeed() < 0.5 || request.getVoiceSpeed() > 2.0)) {
            throw new BusinessException(ErrorCode.PARAM_INVALID);
        }
        if (request.getVoicePitch() != null && (request.getVoicePitch() < 0.5 || request.getVoicePitch() > 2.0)) {
            throw new BusinessException(ErrorCode.PARAM_INVALID);
        }
        if (request.getVoiceVolume() != null && (request.getVoiceVolume() < 0.1 || request.getVoiceVolume() > 1.0)) {
            throw new BusinessException(ErrorCode.PARAM_INVALID);
        }

        // 选择性覆盖
        if (request.getVoiceSpeed() != null) entity.setVoiceSpeed(request.getVoiceSpeed());
        if (request.getVoicePitch() != null) entity.setVoicePitch(request.getVoicePitch());
        if (request.getVoiceVolume() != null) entity.setVoiceVolume(request.getVoiceVolume());
        if (request.getPreferredVoice() != null) entity.setPreferredVoice(request.getPreferredVoice());
        if (request.getLanguagePreference() != null) entity.setLanguagePreference(request.getLanguagePreference());
        if (request.getAutoPlayVoice() != null) entity.setAutoPlayVoice(request.getAutoPlayVoice());
        if (request.getShowTranscript() != null) entity.setShowTranscript(request.getShowTranscript());

        if (entity.getId() == null) {
            userPreferenceMapper.insert(entity);
        } else {
            userPreferenceMapper.updateById(entity);
        }
    }
}


